import 'package:flutter/material.dart';
import 'package:multi_shop/pages/cart.dart';
import 'package:multi_shop/utils/ws.dart';

import 'pages/home.dart';
import 'pages/msg.dart';
import 'pages/my.dart';

class Index extends StatefulWidget {
  const Index({super.key});
  @override
  State<Index> createState() => _IndexState();
}

class _IndexState extends State<Index> {
  int current = 1;
  @override
  void initState() {
    super.initState();
    final uri = Uri.parse(
      'ws://127.0.0.1:8001/ws',
    ).replace(queryParameters: {'role': 'user', 'user_id': '9', 'token': ''});

    chatWs.connect(
      uri: uri,
      params: <String, String>{}..addAll({'role': 'user', 'user_id': '9'}),
    );
  }

  @override
  void dispose() {
    chatWs.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _getPage(current),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: current,
        onTap: (value) => {
          setState(() {
            current = value;
          }),
        },
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart),
            label: "购物车",
          ),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的"),
        ],
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  Widget _getPage(index) {
    final List<Widget> pages = [Home(), ShoppingCart(), Msg(), My()];
    return pages[index];
  }
}
